package com.itguigu.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itguigu.auth.mapper.SysPostMapper;
import com.itguigu.auth.mapper.SysUserMapper;
import com.itguigu.auth.service.SysDeptService;
import com.itguigu.auth.service.SysPostService;
import com.itguigu.auth.service.SysUserService;
import com.itguigu.model.system.SysDept;
import com.itguigu.model.system.SysPost;
import com.itguigu.model.system.SysUser;
import com.itguigu.security.custom.LoginUserInfoHelper;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysDeptService sysDeptService;

    @Resource
    private SysPostService sysPostService;

    @Override
    public SysUser selectUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = baseMapper.selectOne(queryWrapper);
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        if (sysUser.getStatus().intValue() == 0) {
            throw new RuntimeException("账号已停用");
        }

        return sysUser;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        // 根据userid查询用户对象
        SysUser sysUser = baseMapper.selectById(id);
        if (status.intValue() == 1) {
            // 设置修改状态值
            sysUser.setStatus(status);
        } else {
            sysUser.setStatus(0);
        }
        // 调用方法进行修改
        baseMapper.updateById(sysUser);
    }

    @Override
    public SysUser getUserByUserName(String assignee) {
        return baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, assignee));
    }

    /**
     * 我的--获取当前用户地基本信息
     *
     * @return
     */
    @Override
    public Map<String, Object> getCurrentUser() {
        // 根据 用户id 获取用户信息
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());

//        SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
//        SysPost sysPost = sysPostService.getById(sysUser.getPostId());

        HashMap<String, Object> result = new HashMap<>();
        result.put("name", sysUser.getName());
        result.put("phone", sysUser.getPhone());
//        result.put("deptName", sysDept.getName());
//        result.put("postName", sysPost.getName());

        return result;
    }
}
